<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2016/10/20
 * Time: 21:15
 */
namespace App\Http\Controllers\Admin\SystemLog;

use App\Http\Controllers\AdminController;
use Illuminate\Support\Facades\DB;
class SystemLogController extends AdminController
{
    /**
     * 后台日志管理页面...
     * @author  <ZhengYaFeng>
     * @access  <public>
     * @date    <2016-10-29 10:10>
     * @param   <null>
     * @return  <mixed>
     * @des     <描述:后台日志管理页面>
     * @return mixed
     */
    public function index()
    {
        $data = array();
        $data['list'] = DB::table('admini_user_log')->orderBy('create_time', 'desc')->paginate(10);
        return \Helpers::showViewCURD('Admin.SystemLog.index', 3, '', $data);
    }

    public function setSysimg()
    {
        $data = [];
        //定义允许的文件上传类型
        $passUpFileType = array('image/jpg','image/png','image/jpeg','image/gif');
        //定义上传文件路径
        $uploadPath = 'upload/ewm/'.date('Y-m-d').'/';
        $shopData = [];
        if(!empty($_FILES['img1']['name']))
        {
            //商品封面图
            $shop_img = 'img1';
            //定义新的上传文件名
            $newFileNamw = date('YmdHis').uniqid().mt_rand(0,10000);
            //上传文件方法调用
            $upRes = \Helpers::uploadFile($shop_img,$passUpFileType,$uploadPath,$newFileNamw);
            //如果报错返回错误码
            if($upRes < 0) return $upRes;
            $shopData['img1'] = str_replace('\\','/',$upRes);
        }
        if(!empty($_FILES['img2']['name']))
        {
            //商品封面图
            $shop_img1 = 'img2';
            //定义新的上传文件名
            $newFileNamw1 = date('YmdHis').uniqid().mt_rand(0,10000);
            //上传文件方法调用
            $upRes1 = \Helpers::uploadFile($shop_img1,$passUpFileType,$uploadPath,$newFileNamw1);
            //如果报错返回错误码
            if($upRes1 < 0) return $upRes1;
            $shopData['img2'] = str_replace('\\','/',$upRes1);
        }
        if(!empty($_FILES['img3']['name']))
        {
            //商品封面图
            $shop_img2 = 'img3';
            //定义新的上传文件名
            $newFileNamw2 = date('YmdHis').uniqid().mt_rand(0,10000);
            //上传文件方法调用
            $upRes2 = \Helpers::uploadFile($shop_img2,$passUpFileType,$uploadPath,$newFileNamw2);
            //如果报错返回错误码
            if($upRes2 < 0) return $upRes2;
            $shopData['img3'] = str_replace('\\','/',$upRes2);
        }
        if(!empty($shopData))
        {
            //检测数据库是否有过设置数据，没有的新增，有的修改
            $hasInfo = DB::table('system_img')
                        ->first();
            if(empty($hasInfo))
            {
                DB::table('system_img')->insert($shopData);
            }else{
                DB::table('system_img')->where(['id'=>$hasInfo->id])->update($shopData);
            }
            return redirect()->back()->withInput()->withErrors('设置成功!');
        }
        $data[] =  DB::table('system_img')
            ->first();
        return \Helpers::showViewCURD('Admin.SystemLog.sysset', 1, '设置', $data);
    }
}